package da.metricscraft.threads;

import java.util.Stack;

import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

import da.metricscraft.MetricsCraft;
import da.metricscraft.information.MovementInformation;
import da.metricscraft.information.MovementInformation.MoveActions;

public class MovementHandler extends Thread {
	private static MovementHandler handler;
	public static MovementHandler getInstance(){
		return handler;
	}
	public static class MovementStruct{
		public MovementStruct(Player p, Integer lifetime, Vector position, MoveActions action){
			this.player = p;
			this.lifeTime = lifetime;
			this.position = position;
			this.action = action;
		}
		public MoveActions action;
		public Player player;
		public int lifeTime;
		public Vector position;
	}
	private Stack<MovementStruct> movements;
	//private ArrayList<Player, Vector>
	public MovementHandler(){
		movements = new Stack<MovementStruct>();
		MovementHandler.handler = this;
	}
	@Override
	public void run() {
			while(true){
				if (!movements.isEmpty()){
					MovementStruct sn = movements.pop();
					MetricsCraft.infoGatherer.putInformation(new MovementInformation(sn.player.getDisplayName(), sn.position, sn.lifeTime, sn.action));		
				}else
					try {
						synchronized(this){
							this.wait();
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
			}
	}
	public boolean isEmpty(){
		return movements.isEmpty();
	}
	public synchronized void addMovement(MovementStruct sn){
		movements.add(sn);
		this.notifyAll();
	}
}
